package com.jiyi.common;

import java.util.Map;

import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request.Method;
import com.android.volley.RequestQueue;
import com.android.volley.RetryPolicy;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

public class VolleyRequest {
	
	
	public static StringRequest stringRequest;
	private static RequestQueue queue;
	private static JsonObjectRequest objectRequest;
	  
	private static RequestQueue getHttpQueue(Context context){
		
		queue = Volley.newRequestQueue(context);
		return queue;
	}    	


	/**
	 * get 请求 
	 * return string
	 * 
	 * @param context
	 * @param url
	 * @param tag
	 * @param listener
	 */

    public static void StringRequestGet(Context mContext, String url, String tag,StringInterface vif) {
    	
        stringRequest = new StringRequest(Method.GET, url,vif.stringListener(), vif.errorListener());
        stringRequest.setTag(tag);
        getHttpQueue(mContext).add(stringRequest);
        
    }
    
    /**
     * get 请求
     * return JSONObject
     * 
     * @param context
     * @param url
     * @param tag
     * @param listener
     * */
    public static void jsonRequestGet(Context mContext,String url,String tag,JsonInterface<JSONObject> vif){
    	objectRequest = new JsonObjectRequest(Method.GET, url, null, vif.JsonListener(), vif.errorListener());
    	objectRequest.setTag(tag);
    	getHttpQueue(mContext).add(objectRequest);
    }
    
    
    
    /**
	 * post 请求 
	 * return string
	 * 
	 * @param context
	 * @param url
	 * @param tag
	 * @param map
	 * @param listener
	 */
    public static void StringRequestPost(Context mContext, String url, String tag,
            final Map<String, String> params, StringInterface vif) {

        stringRequest = new StringRequest(Method.POST, url,vif.stringListener(), vif.errorListener()) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return params;
            }
        };
        stringRequest.setTag(tag);       
        getHttpQueue(mContext).add(stringRequest);
    }
    
    
    /**
     * 销毁
     *
     * @param tag
     * */
    public static void cancelRequest(String tag){
    	queue.cancelAll(tag);
    }

}
